package com.docios.cookbook2.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.docios.cookbook2.R;
import com.docios.cookbook2.entity.QueryBean;
import com.docios.cookbook2.ui.otherPager.QueryDetailActivity;
import com.docios.cookbook2.utils.NetUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ItemCommonFoodAdapter extends RecyclerView.Adapter<ItemCommonFoodAdapter.ViewHolder> {
    private Context cContext;
    private List<QueryBean> cQueryBeans;
    String id;
    String appcode = "APPCODE 30dd63e48deb4d88b0ffcd5c40c6ae86";

    public ItemCommonFoodAdapter(Context cContext, List<QueryBean> cQueryBeans) {
        this.cContext = cContext;
        this.cQueryBeans = cQueryBeans;
    }

    @NonNull
    @Override
    public ItemCommonFoodAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(cContext).inflate(R.layout.item_common_food, parent, false);
        return new ViewHolder(view);
    }

    /*
     * 存入数据和交互
     * */
    @Override
    public void onBindViewHolder(@NonNull ItemCommonFoodAdapter.ViewHolder holder, int position) {
        //存入菜名,内容和封面
        holder.name.setText(cQueryBeans.get(position).getName());
        holder.content.setText(cQueryBeans.get(position).getContent());

        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.drawable.food_picture_null)
                .error(R.drawable.food_picture_null)
                .fallback(R.drawable.food_picture_null);
        Glide.with(cContext)
                .load(cQueryBeans.get(position).getPic())
                .apply(requestOptions)
                .into(holder.coverImg);

        //进入详情页
        holder.coverImg.setOnClickListener(v -> {
            //判断数据是否有null值（来自本地数据库）：是，id查询。否，直接跳转。
            if (cQueryBeans.get(position).getProcessBeans() == null ||
                    cQueryBeans.get(position).getMaterialBeans() == null) {

                id=cQueryBeans.get(position).getId();
                Map<String, String> map = new HashMap<>();
                map.put("host", "https://jsucpdq.market.alicloudapi.com/recipe/detail?id=%s");
                map.put("id", id);
                map.put("appcode", appcode);

                NetUtil.queryObjectFromAli(map, new NetUtil.OnObjectQueriedCallBack() {
                    @Override
                    public void onSuccess(QueryBean recipe) {
                        if (recipe==null) {
                            onFailure(new Exception("data not queried"));
                            return;
                        }

                        Intent intent = new Intent(cContext, QueryDetailActivity.class);
                        intent.putExtra("data", recipe);
                        cContext.startActivity(intent);
                    }

                    @Override
                    public void onFailure(Exception e) {
                        new Handler().post(() -> Toast.makeText(cContext, "查询失败", Toast.LENGTH_SHORT).show());
                    }
                });
                map.clear();

            }else {
                Intent intent = new Intent(cContext, QueryDetailActivity.class);
                intent.putExtra("data", cQueryBeans.get(position));
                cContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return cQueryBeans.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView coverImg;
        TextView name;
        TextView content;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            coverImg = itemView.findViewById(R.id.item_com_food_img);
            name = itemView.findViewById(R.id.item_com_food_name);
            content = itemView.findViewById(R.id.item_com_food_content);
        }
    }

    //替换数据
    @SuppressLint("NotifyDataSetChanged")
    public void notifyChange() {
        notifyDataSetChanged();
    }



}
